//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.dao.wills;

import com.aidong.model.wills.DicShopCode;
import com.aidong.model.wills.PtItemCode;
import com.aidong.model.wills.PtPrice;

import java.util.Iterator;
import java.util.List;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

@Repository
public class PtItemCodeDao extends BaseDao<PtItemCode, String> {
    public PtItemCodeDao() {
    }

    public List<PtItemCode> getValidItemList() {
        String sql = "select code, chName from PtItemCode where status=1 and ActiveTime<getdate() and InactiveTime>getdate()";
        return this.queryListBySql(PtItemCode.class, sql, new Object[0]);
    }

    public List<PtItemCode> getShopItemList(String shopCode) {
        String sql = "select code, chName from PtItemCode ic inner join PtShop s on ic.code=s.ptCode\nwhere shopCode=:p1 and status=1 and ActiveTime<getdate() and InactiveTime>getdate()";
        return this.queryListBySql(PtItemCode.class, sql, new Object[]{shopCode});
    }

    public List<PtItemCode> getShopItemList(String shopCode, String itemType) {
        String sql = "select code, chName from PtItemCode ic inner join PtShop s on ic.code=s.ptCode\nwhere shopCode=:p1 and itemType=:p2 and status=1 and ActiveTime<getdate() and InactiveTime>getdate()";
        return this.queryListBySql(PtItemCode.class, sql, new Object[]{shopCode, itemType});
    }

    public List<DicShopCode> getPtShopList(String code, boolean ball) {
        String sql;
        if (ball) {
            sql = "select s.code, s.chName, (case when a.shopCode is null then 0 else 1 end) status from DicShopCode s\nleft join PtShop a on s.code=a.shopCode and ptCode = :p1\nwhere status>0 order by s.code";
        } else {
            sql = "select s.code, s.chName, status from DicShopCode s\ninner join PtShop a on s.code=a.shopCode\nwhere ptCode = :p1 and status>0 order by s.code";
        }

        return this.queryListBySql(DicShopCode.class, sql, new Object[]{code});
    }

    public void addPtItem(PtItemCode ptItem, List<String> shopList, List<PtPrice> priceList) {
        this.add(ptItem);
        this.saveShopPrice(ptItem.getCode(), shopList, priceList);
    }

    public void updatePtItem(PtItemCode ptItem, List<String> shopList, List<PtPrice> priceList) {
        this.update(ptItem);
        this.saveShopPrice(ptItem.getCode(), shopList, priceList);
    }

    private void saveShopPrice(String ptCode, List<String> shopList, List<PtPrice> priceList) {
        Session session = this.getSession();
        SQLQuery query = session.createSQLQuery("delete from PtShop where ptCode = :p1");
        query.setString("p1", ptCode);
        query.executeUpdate();
        query = session.createSQLQuery("insert into PtShop (ptCode, ShopCode) values (:p1, :p2)");
        query.setString("p1", ptCode);
        Iterator i$ = shopList.iterator();

        while (i$.hasNext()) {
            String price = (String) i$.next();
            query.setString("p2", price);
            query.executeUpdate();
        }

        i$ = priceList.iterator();

        while (i$.hasNext()) {
            PtPrice price1 = (PtPrice) i$.next();
            if ("-".equals(price1.getPtCode())) {
                PtPrice tmp = (PtPrice) this.getSession().get(PtPrice.class, price1.getAutoId());
                this.getSession().delete(tmp);
            } else {
                price1.setPtCode(ptCode);
                this.getSession().saveOrUpdate(price1);
            }
        }

    }

    public boolean updateStatus(String code, int status) {
        String hql = "update PtItemCode set status=:p1 where code=:p2";
        return this.updateByHql(hql, new Object[]{Integer.valueOf(status), code}) > 0;
    }
}
